***************************************************
* Step 1: Get BASEVAR from H97 - household file   *
***************************************************;
data basevar; 
   set meps2005.h97 (rename=(PERWT05F = PERWT INSCOV05 = INSCOV)); 
************* 
* Gender    *
*************;      
        if sex = 2 then female=1; 
   else if sex = 1 then female=2; 
       ******************
       * Race/Ethnicity *
       ******************;
     if (racethnx = 1)    then race = 3; * Hispanic; 
else if (racethnx = 2)    then race = 2; * Black;
else if (racex   = 1 )    then race = 1; * White;
else                           race = 4; * Other;

***************
* Age renamed *
***************;
age = age05x;
 if (age < 0) then age = .;
Label age = "Continuous age";
 *********************
 * Age grouped       *
 * -1 inapplicable   *
 *********************;
       if (0  <= age <= 21)  then age_grp = 0;
  else if (22 <= age <= 39)  then age_grp = 1;
  else if (40 <= age <= 49)  then age_grp = 2;     
  else if (50 <= age <= 64)  then age_grp = 3;
  else if (65 <= age      )  then age_grp = 4;
label age_grp = "Age grp 0,22,40,50,65";
 **************************
 * Marital status recoded *
 **************************;
       if MARRY05X = 1          then marr =1; * married; 
  else if MARRY05X = 2          then marr =2; * widowed; 
  else if 3<= MARRY05X <=4      then marr =3; * separated/divorced; 
  else if MARRY05X = 5          then marr =4; * Never Married; 
label marr = "Marital status";
 ***********************
 * Living Arrangement  *
 ***********************;
       if (spouin05 in (3,-9))    then live_with_spouse = .;
  else if (spouin05 = 1)          then live_with_spouse = 1; * Living with Spouse ; 
  else if (spouin05 in (2))       then live_with_spouse = 2; * Not Living with Spouse; 
  *****************************
  * education                 *
  * Change in variable name   *
  ******************************;
       if ( 0 <= educyr <= 11)      then educ = 1; /* LT HS */
  else if ( educyr = 12)            then educ = 2; /* HS */
  else if ( 13 <= educyr <= 17)     then educ = 3; /* some College + */
  **********************
 * Alive or dead      *
 **********************;
 if PSTATS31 in (31) ! 
    PSTATS42 in (31) ! 
    PSTATS53 in (24, 31) then died = 1;
  else died=2;
label died = "Died";
       *******************
       * Census region   *
       *******************;
      if (region05 = -1) then region = .;
  else                        region = region05;
    ****************
    * MS/Metro     *
    ****************;
       if (msa05 < 0)   then metro = .;
  else if (msa05 = 0)   then metro = 2;
  else if (msa05 = 1)   then metro = 1;
    ********************
    * Poverty line     *
    ********************;
label poor = "Income FPL recoded";       
        if (povcat05 =1)        then poor = 1;    /* <100-poor */
   else if (povcat05 in (2,3))  then poor = 2;    /* 100-199 - Near Poor */
   else if (povcat05 = 4)       then poor = 3;    /* 200-399 - Middle Income */
   else if (povcat05 = 5)       then poor = 4;    /* 400+ - High Income */
   ********************
   * Employment Status*
   ********************;
  employ  = empst53;
  if (employ < 0 ) then employ  = empst42;
  if (employ < 0 ) then employ  = empst31;
        if (employ in (1,2,3)) then employ = 1; * Ever Employ; 
   else if (employ = 4)        then employ = 2; * Not Employ; 
   else                             employ = .; * Missing;
label employ = "Employed";
   ***********************************************
   * Health Insurance --- can use INSCOV As well *
   ***********************************************;
          if (unins05 = 1) then hlthins = 4; * NO INSURANCE; 
     else if (unins05 = 2) then do;
          if (triev05 = 1) then hlthins = 1; * MILITARY INSRUANCE; 
     else if (prvev05 = 1) then hlthins = 2; * Any PRIVATE; 
     else if (mcrev05 = 1 ! mcdev05 = 1 ! opaev05 = 1 ! opbev05 = 1 ) 
     then hlthins = 3; * ONLY PUBLIC; 
   end;
label hlthins = "Hlth ins grp";
******************
*Currently Smoker*
******************;
curr_smk = adsmok42;
if (ADSMOK42 < 0) then curr_smk = .;

   ******************
   * Body mass index*
   ******************;
           if (0  <= BMINDX53 < 18.5) then bmi = 1; /* Underweight */
    else if (18.5 <= BMINDX53 < 25.0) then bmi = 2; /* Normal      */
    else if (25.0 <= BMINDX53 < 30.0) then bmi = 3; /* Overweight  */
    else if (30.0 <= BMINDX53       ) then bmi = 4; /* Obese       */
   ******************************* 
   *self-report medical condition*
   *******************************;
     if  CHDDX53 =1 ! ANGIDX53 =1 ! MIDX53 =1 ! OHRTDX53 =1  then HEART_SELF =1; 
else if (CHDDX53 =2 & ANGIDX53 =2 & MIDX53 =2 & OHRTDX53 =2) then HEART_SELF =2;
     if (DIABDX53 = 1) then DIAB_SELF = 1;  
else if (DIABDX53 = 2) then DIAB_SELF = 2;

  
     ************************************   
     * Percieved Physical Health status *
     * NO -3 in 2004, 2005              *
     ************************************;
  if (RTHLTH31 in (-1,-7,-8,-9)) then rthlth31 = .;
  if (RTHLTH42 in (-1,-7,-8,-9)) then rthlth42 = .;
  if (RTHLTH53 in (-1,-7,-8,-9)) then rthlth53 = .;
           *******************************
   if (rthlth53 = .) then rthlth53 = rthlth42; /* THESE TWO STMTS */
  if (rthlth53 = .) then rthlth53 = rthlth31; /* ARE REQUIRED    */

        if (rthlth53 in (1,2)) then health = 1;
   else if (rthlth53 = 3     ) then health = 2;
   else if (rthlth53 in (4,5)) then health = 3;
label health = "Phys hlth";
    **************************
    *Perceived Mental Health *
    * No -3 in 2004          *
    **************************;
  if (MNHLTH31 in (-1,-7,-8,-9)) then mnhlth31 = .;
  if (MNHLTH42 in (-1,-7,-8,-9)) then mnhlth42 = .;
  if (MNHLTH53 in (-1,-7,-8,-9)) then mnhlth53 = .;

  if (mnhlth53 = .) then mnhlth53 = mnhlth42; /* THESE TWO STMTS */
  if (mnhlth53 = .) then mnhlth53 = mnhlth31; /* ARE REQUIRED    */

        if (mnhlth53 in (1,2)) then m_health = 1;
   else if (mnhlth53 = 3     ) then m_health = 2;
   else if (mnhlth53 in (4,5)) then m_health = 3;
label m_health = "Mental health";

length exercise 3.; label exercise = "mod/vig 3x/wk";
     if (PHYACT53 = 1) then exercise = 1;
else if (phyact53 = 2) then exercise = 2;
   *****************************
  * define eligible population*
  * 1: aged 21 and over       *
  ****************************;
length eligpop 3.; label eligpop = "Alive, > 21 years";
  if age05x > 21 & died = 2  then eligpop=1; else eligpop=0;
           **************************
           * Disability             *
           **************************;
length disab_pop 3.; label disab_pop = " Alive 21 < age < 62";
  if 21 < age05x <  62 & died = 2  then disab_pop =1; else disab_pop =0;


       *********************************
       * Flag neagative income (N = 20)*
       * zero income                   *
       *--------------------------------------------------*
       * if (oop = 0 & inc_flg = 0 ) NO execution         *
       * (But the program will complain division by zero) *
       * if (oop = 0 & inc_flg = -1) then oop_exp = 0     *
       * if (oop = 0 & inc_flg >  0) then oop_exp = 0     *
       *--------------------------------------------------*
       * if (oop > 0 & inc_flg = 0) No execution          *
       * if (oop > 0 & inc_flg = -1) then oop_exp = -ve   *
       * if (oop > 0 & inc_flg =  1) then oop_exp = +ve   *
       *--------------------------------------------------* 
       * if (oop < 0 & inc_flg = 0) No execution          *
       * if (oop < 0 & inc_flg = -1) then oop_exp = +ve   *
       * if (oop < 0 & inc_flg = 1)  then oop_exp = -ve   *
       ****************************************************;
length inc_flg 3.; label inc_flg = "Income -ve,zero,+ve";
  income = ttlp05x;
       if (income < 0) then inc_flg = -1;
  else if (income = 0) then inc_flg = 0;
  else if (income > 0) then inc_flg = 1;
  *************************************************
  * Total expenditures, oop,oopshare exp,income   *
  *************************************************;
label tot_exp = "Total expenditures"
      tot_oop = "Total OOP"
      tot_oop_exp = "Total OOP/EXP"
      tot_oop_inc  = "Total OOP/Inc";
tot_exp = TOTEXP05;
tot_oop = TOTSLF05;
     if (tot_exp > 0) then tot_oop_exp = (tot_oop/tot_exp)*100;
else if (tot_exp = 0) then tot_oop_exp = 0;
     if (income ne 0) then tot_oop_inc = (tot_oop/income)*100;
  *************************************************
  * Inp expenditures, oop,oopshare exp,income     *
  * includes zero night stays                     *
  *************************************************;
label inp_exp = "Sum IPFEXP,IPDEXP"
      inp_oop = "Sum IPFSLF,IPDSLF"
      inp_oop_exp = "Inp OOP/EXP"
      inp_oop_inc  = "Inp OOP/Inc";
inp_exp = sum(of IPFEXP05 IPDEXP05);
inp_oop = sum(of IPFSLF05 IPDSLF05);
     if (inp_exp > 0) then inp_oop_exp = (inp_oop/inp_exp) * 100;
else if (inp_exp = 0) then inp_oop_exp = 0;
     if (income ne 0) then inp_oop_inc = (inp_oop/income)*100;
  *************************************************
  * OTP expenditures, oop,oopshare exp,income     *
  * includes office based and outp clinic         *
  *************************************************;
otp_exp = sum(of OBVEXP05 OPFEXP05 OPDEXP05);
otp_oop = sum(of OBVSLF05 OPFSLF05 OPDSLF05);
     if (otp_exp > 0) then otp_oop_exp = (otp_oop/otp_exp)*100; 
else if (otp_exp = 0) then otp_oop_exp = 0;
     if (income ne 0) then otp_oop_inc = (otp_oop/income)*100;
label otp_exp = "Sum OBVEXP,OPFEXP,OPDEXP"
      otp_oop = "Sum OBVSLF,OPFSLF,OPDSLF"
      otp_oop_exp = "outpat OOP/EXP"
      otp_oop_inc  = "outpat OOP/Inc";
  *************************************************
  * Prescription Drug expenditures                *
  *************************************************;
label rx_exp = "Rx expenditures"
      rx_oop = "Rx OOP"
      rx_oop_exp = "RX OOP/EXP"
      rx_oop_inc  = "RX OOP/Inc";
rx_exp = RXEXP05; 
rx_oop = RXSLF05; 
     if (rx_exp > 0)  then rx_oop_exp = (rx_oop/rx_exp)*100; 
else if (rx_exp = 0)  then rx_oop_exp = 0;
     if (income ne 0) then rx_oop_inc = (rx_oop/income)*100;
  *************************************************
  * Emergency room expenditures,oop,oopshare exp  *
  *************************************************;
label er_exp = "Sum ERFEXP,ERDEXP"
      er_oop = "Sum ERFSLF,ERDSLF"
      er_oop_exp = "ER OOP/EXP"
      er_oop_inc  = "ER OOP/Inc";
er_exp = sum(of ERFEXP05 ERDEXP05);
er_oop = sum(of ERFSLF05 ERDSLF05);
     if (er_exp > 0)  then er_oop_exp = (er_oop/er_exp)*100;
else if (er_exp = 0)  then er_oop_exp = 0;
     if (income ne 0) then er_oop_inc = (er_oop/income)*100;
  **********************************************
  * Home health expenditures,oop,oopshare exp  *
  **********************************************;
label hha_exp = "Sum HHAFEXP,HHNEXP"
      hha_oop = "Sum HHASLF,HHNSLF"
      hha_oop_exp = "HHA OOP/EXP"
      hha_oop_inc  = "HHA OOP/Inc";
hha_exp = sum(of HHAEXP05 HHNEXP05);
hha_oop = sum(of HHASLF05 HHNSLF05);
     if (hha_exp > 0) then hha_oop_exp = (hha_oop/hha_exp)*100;
else if (hha_exp = 0) then hha_oop_exp = 0;
     if (income ne 0) then hha_oop_inc = (hha_oop/income)*100;
  *****************************************
  * Dental expenditures,oop,oopshare exp  *
  *****************************************;
label dtl_exp = "Dental Expenditures"
      dtl_oop = "Dental OOP"
      dtl_oop_exp = "Dental OOP/EXP"
      dtl_oop_inc  = "Dental OOP/Inc";
dtl_exp = DVTEXP05; 
dtl_oop = DVTSLF05;
     if (dtl_exp > 0) then dtl_oop_exp = (dtl_oop/dtl_exp)*100;
else if (dtl_Exp = 0) then dtl_oop_exp = 0;
     if (income ne 0) then dtl_oop_inc = (dtl_oop/income)*100;
  *****************************************
  * Vision expenditures,oop,oopshare exp  *
  *****************************************;
label vis_exp  = "Vision Expenditures"
      vis_oop = "Vision OOP"
      vis_oop_exp = "Vision OOP/EXP"
      vis_oop_inc  = "Vision OOP/Inc";
vis_exp = VISEXP05;
vis_oop = VISSLF05;
      if (vis_exp > 0) then vis_oop_exp = (vis_oop/vis_exp)* 100;
 else if (vis_exp = 0) then vis_oop_exp = 0;
      if (income ne 0) then vis_oop_inc = (vis_oop/income)*100;
  *****************************************
  * Other expenditures,oop,oopshare exp  *
  *****************************************;
label oth_exp = "Other (DME) Expenditures"
      oth_oop = "Other (DME) OOP"
      oth_oop_exp = "Other (DME) OOP/EXP"
      oth_oop_inc  = "Other (DME) OOP/Inc";
oth_exp = OTHEXP05;
oth_oop = OTHSLF05;
     if (oth_exp > 0) then oth_oop_exp = (oth_oop/oth_exp)*100;
else if (oth_exp = 0) then oth_oop_exp = 0;
     if (income ne 0) then oth_oop_inc = (oth_oop/income)*100;
**************************************
* Computed other expenditures        *
**************************************;
label hdvo_exp = "Sum of hha,dtl,vis,oth exp"
      hdvo_oop = "Sum of hha,dtl,vis,oth OOP"
      hdvo_oop_exp = "HDVO OOP/EXP"
      hdvo_oop_inc  = "HDVO OOP/Inc";
   hdvo_exp = sum (of hha_exp dtl_exp vis_exp oth_exp);
   hdvo_oop = sum (of hha_oop dtl_oop vis_oop oth_oop);
     if (hdvo_exp > 0) then hdvo_oop_exp = (hdvo_oop / hdvo_exp)*100;
else if (hdvo_exp = 0) then hdvo_oop_exp = 0;
     if (income ne 0) then hdvo_oop_inc = (hdvo_oop/income)*100;

       *********************
       * Total outp visits *
       *********************;
length tot_otp_vis 3.; label tot_otp_vis = "Sum obtotv optotv";
   tot_otp_vis = sum(of obtotv05 optotv05);

****************
*Year of MEPS  *
****************;
length meps_year 5.;
label meps_year = "Year of MEPS";
meps_year = 2005;

keep   FEMALE RACE AGE_GRP MARR LIVE_WITH_SPOUSE 
       EDUC EDUCYR EMPLOY POOR 
       METRO REGION 
       INSCOV HLTHINS 
       USC USC_TYPE 
       CURR_SMK BMI exercise
       HEALTH M_HEALTH 
       HEART_SELF DIAB_SELF BP_SELF
       died age eligpop meps_year 
       tot_exp tot_oop tot_oop_exp tot_oop_inc
       inp_exp inp_oop inp_oop_exp inp_oop_inc
       otp_exp otp_oop otp_oop_exp otp_oop_inc
        rx_exp  rx_oop  rx_oop_exp  rx_oop_inc
        er_exp  er_oop  er_oop_exp  er_oop_inc
       hha_exp hha_oop hha_oop_exp hha_oop_inc
       dtl_exp dtl_oop dtl_oop_exp dtl_oop_inc
       vis_exp vis_oop vis_oop_exp vis_oop_inc
       oth_exp oth_oop oth_oop_exp oth_oop_inc
       hdvo_exp hdvo_oop hdvo_oop_exp hdvo_oop_inc
       income inc_flg tot_otp_vis va_amt nonva_amt mcmapvt_amt 
       disab_pop disab_adl disab_act disab_func disab_ben
       disab_ssdi disab_ssi disab_vet disab_wc 
       veteran va_use1 
       DUPERSID PERWT VARSTR VARPSU
       mam_test pap_test chol_test flu_shot; 

length FEMALE RACE AGE_GRP MARR LIVE_WITH_SPOUSE 
       EDUC EMPLOY POOR 
       METRO REGION 
       INSCOV HLTHINS 
       USC USC_TYPE 
       CURR_SMK BMI exercise
       HEALTH M_HEALTH 
       HEART_SELF DIAB_SELF BP_SELF  
       died age eligpop disab_pop
       disab_pop disab_adl disab_act disab_func disab_ben
       disab_ssdi disab_ssi disab_vet disab_wc 
       veteran va_use1  3.; 
        
proc sort; by dupersid; 


data meps2005.basevar_2005; set basevar;
proc contents data=meps2005.basevar_2005;
title "Contents Base variables for MEPS 2005";

proc format;
    value yesno   1 = "Yes" 2 = "No";
    value female  1 = "Female" 2 = "Men";
    value racefmt 1 = "White" 2 = "Afr Am" 3 = "Latino" 4 = "Other";
    value empfmt  1 = "Employed" 2 = "Not employed";
    value hlthfmt 1 = "Ex/vgood" 2 = "Good" 3 = "Fair/poor";
    value educfmt 1 = "LT HS" 2 = "HS" 3="> HS";
    value marrfmt 1 = "Married" 2 = "Widow" 3="Sep/Div" 4 = "Never marr";
    value insfmt  1 = "Private" 2 = "Public" 3 = "Unins";
    value povfmt  1 = "Poor" 2 = "Near Poor" 3="Middle Income" 4="High Income";
    value uscfmt  1 = "USC" 2 = "NO USC";
    value usctype 1 = "PCP" 2 = "Non-PCP" 3 = "Other" 4 = "NO USC";
    value region  1 = "Northeast" 2 = "Mid west" 
                          3 = "South" 4 = "West";
    value agefmt  0 = "0 - 21" 1 = "22 - 29" 
                  2 = "30-45" 3="46-64" 4= "65,+";
    value metro   1 = "METRO" 2 = "RURAL";
    value bmifmt  1 = "Under wt" 2 = "Normal" 3 = "Over" 
                  4 = "Obese" 5 = "Missing";
    value exercise 1 = "3/wk" 2 = "No exer";
    value cancer  1 = "Caner" 2 = "No cancer";
    value asthma  1 = "Asthma" 2 = "No asthma";
    value depfmt   1 = "Deprn" 2 = "No DEPRN";
    value anxiety 1 = "Anxiety" 2 = "No anxiety";
    value arthritis 1 = "Arthritis" 2 = "No arthri";
    value bpfmt 1 = "Hypertension" 2 = "NO BP";
    value emphy 1 = "Emphysema" 2 = "NO emphy";
    value osteo 1 = "Osteoporosis" 2 = "No osteo";
    value currsmk 1 = "Curr smoker" 2 = "Other";
    value diabetes 1 = "Diabetes" 2 = "No diabetes";
    value heart 1 = "heart dis" 2 = "No heart dis";
    value sabuse 1 = "Sabuse" 2 = "No sabuse";
    value mifmt 1 = "Other MI" 2 = "No oth mi";
    value disabpop 1 = "Work age 22-61" 0 = "No wrk age";
    value disabadl 1 = "Disab ADL" 2 = "No ADL disab";
    value disabfun 1 = "Disab Func" 2 = "NO fun disab";
    value disaben 1 = "Disab benefit" 2 = "No disab benefit";
    value disabact 1  ="Disab Activi" 2 = "No act limit";
    value veteran 1 = "Veteran" 2 ="Non-vet";
    value vause   1 = "VA-user" 2 = "Non-va" 3 = "Non-VET";
    value obvause 1 = "OB-VA" 2 = "Non oBVA";
    value otpvause 1 = "OTP-VA" 2 = "NO OTP VA";
    value ervause  1 = "ER-VA"  2 = "No ER VA";
    value inpvause 1 = "INP VA" 2 = "No INP VA";
    value diabrx 1 = "Antidiabetic" 2 = "No anti-db";
    value htnrx  1 = "HTN Rx"  2= "NO HTN rx";
    value psyrx   1 = "PSY RX"  2 = "No psy RX";
    value heartrx 1 = "Oth Hrt rx" 2 = "no heart rx";
    value lipidrx 1 = "Anti-lipid" 2 = "No anti-lipid";
    value painrx  1 = "Pain med" 2 = "No Pain med";
    value adprx   1 = "Anti Dep" 2 = "No ADP";
    value paptest 1 = "PAP < 2 yrs" 2 = "No PAP";
    value mamtest 1 = "MAM < 2 yrs" 2 = "No MAM";
    value choltest 1 = "Chol < 5 yrs" 2 = "No CHOL";
    value flushot 1 = "Flu < 1 yr" 2 = "No FLUSHOT";
    value incflag  -1 = "-VE income" 0 = "Zero Inc" 1 = "+VE Income";


proc freq; tables
       FEMALE RACE AGE_GRP MARR LIVE_WITH_SPOUSE 
       EDUC EMPLOY POOR  METRO REGION 
       INSCOV HLTHINS  USC USC_TYPE 
       CURR_SMK BMI exercise HEALTH M_HEALTH 
       died meps_year
       disab_pop disab_adl disab_act disab_func disab_ben
       disab_ssdi disab_ssi disab_vet disab_wc 
       veteran va_use va_use1
       ob_va_use otp_va_use er_va_use inp_va_use
       diab_rx lipid_rx heart_rx htn_rx psy_rx pain_rx pain_rx1 adp_rx
       pap_test mam_test chol_test flu_shot inc_flg;
 format female female. age_grp agefmt.
 race racefmt. marr marrfmt.  educ educfmt. 
 metro metro.  region region.  poor povfmt.
 employ empfmt.  inscov insfmt. usc yesno.
 usc_type usctype. health hlthfmt. 
 m_health hlthfmt. bmi bmifmt. curr_smk currsmk.
 exercise exercise.
 cancer cancer. asthma asthma. bp bpfmt. diabetes diabetes.
 dep depfmt. anxiety anxiety. heart heart. mental_ill mifmt.
 sabuse sabuse.
 disab_pop disabpop. disab_adl disabadl. disab_func disabfun.
 disab_act disabact. disab_ben disaben.
 veteran veteran. va_use va_use1 vause.
 ob_va_use obvause. otp_va_use otpvause. 
 er_va_use ervause. inp_va_use inpvause.
 diab_rx diabrx. htn_rx htnrx. lipid_rx lipidrx.
 heart_rx heartrx. pain_rx pain_rx1 painrx. adp_rx adprx.
 psy_rx psyrx.
 pap_test paptest. mam_test mamtest. chol_test choltest.
 flu_shot flushot. inc_flg incflag.;

proc means maxdec=0;
var    age
       tot_exp tot_oop tot_oop_exp tot_oop_inc
       inp_exp inp_oop inp_oop_exp inp_oop_inc
       otp_exp otp_oop otp_oop_exp otp_oop_inc
        rx_exp  rx_oop  rx_oop_exp  rx_oop_inc
        er_exp  er_oop  er_oop_exp  er_oop_inc
       hha_exp hha_oop hha_oop_exp hha_oop_inc
       dtl_exp dtl_oop dtl_oop_exp dtl_oop_inc
       vis_exp vis_oop vis_oop_exp vis_oop_inc
       oth_exp oth_oop oth_oop_exp oth_oop_inc
       hdvo_exp hdvo_oop hdvo_oop_exp hdvo_oop_inc
       income tot_otp_vis va_amt nonva_amt mcmapvt_amt 
       diab_rx_exp lipid_rx_exp htn_rx_exp heart_rx_exp psy_rx_exp adp_rx_exp
       diab_rx_oop lipid_rx_oop htn_rx_oop heart_rx_oop psy_rx_oop adp_rx_oop;
run;


libname meps2005 'E:\MEPS\new_basevar_2005';
libname old 'E:\MEPS\basevar_2005';

data add; 
   set meps2005.h97 (keep= DUPERSID OCCCAT31 OCCCAT42 OCCCAT53 INDCAT31 INDCAT42 INDCAT53); 

OCCCAT=OCCCAT31;
if OCCCAT<0 then OCCCAT=OCCCAT42;
if OCCCAT<0 then OCCCAT=OCCCAT53;

INDCAT=INDCAT31;
if INDCAT<0 then INDCAT=INDCAT42;
if INDCAT<0 then INDCAT=INDCAT53;

run;

proc freq data=try;
tables OCCCAT INDCAT/missing;
run;

data obese; 
   set old.basevar_2005;
            *****************************
            * a) chronic mental illness *
            * b) chronic physic illness *
            * C) BMI > 30               *
            * d) tot exp >= 0           *
            * e) No public insurance    *
            *****************************;
length obese_pop 3.; label obese_pop = "Obese,phy & ment ill";
  obese_pop = 0;
  if (eligpop =1) & 
     (bp    = 1 ! heart = 1 ! diabetes = 1 ! osteo_arth = 1 !
      asthma = 1 ) &
      (bmi =4) & (tot_exp >= 0) &
	  (inscov ne 2) then obese_pop = 1;
  else obese_pop = 0;
*************************
* Recode zeros to 2     *
*************************;
 array recodes  cancer
     heart bp diabetes osteo_arth asthma 
     schiz anxiety dep mental_ill sabuse;
   do over recodes;
      if (recodes in (.,0)) then recodes = 2;
   end;
length dep_anx_sz 3.; label dep_anx_sz = "Any dep,anx,sz";
  if (dep = 1 ! anxiety = 1 ! schiz = 1) then dep_anx_sz = 1;
  else                                        dep_anx_sz = 2;

run;

data check;
set obese;
if obese_pop = 1 and PERWT>0 ;
run;


proc freq data=old.Basevar_2005(keep=alzhmr asthma arthritis 
cancer
emphysema diabetes heart bp
osteo stroke);
tables alzhmr asthma arthritis 
cancer
emphysema diabetes heart bp
osteo stroke;
run;

data adult_tot_05;
	merge old.Basevar_2005(keep=DUPERSID tot_exp female race age marr educyr metro income CURR_SMK 
exercise INSCOV employ alzhmr asthma arthritis cancer emphysema diabetes heart bp osteo stroke)
 check(keep=DUPERSID dep_anx_sz  in=A);
 by DUPERSID;
 if A;
if 21<age<65 & tot_exp>=0 & female>=0 & race>=0 & marr>=0 & educyr>=0 & metro>=0 & income>=0 & CURR_SMK>=0 & 
exercise>=0 & dep_anx_sz>=0 & INSCOV>=0 & employ>=0 & alzhmr>=0 & asthma>=0 & arthritis>=0 & cancer>=0 & 
emphysema>=0 &  diabetes>=0 &  heart>=0 &  bp>=0 &  osteo>=0 &  stroke>=0;

female=2-female;
metro=2-metro;
exercise=2-exercise;
CURR_SMK=2-CURR_SMK;
dep_anx_sz=2-dep_anx_sz;
employ=2-employ;

if inscov=3 then inscov=0;

/*if race=1 then white=1; else white=0;*/
if race=2 then black=1; else black=0;
if race=3 then hispc=1; else hispc=0;
if race=4 then otherace=1; else otherace=0;

if marr=1 then married=1; else married=0;

***************************
* # of comorbid conditions*
***************************;
comorb_nbr = sum (alzhmr,asthma,arthritis,cancer,emphysema,diabetes,heart,bp,osteo,stroke);
if (comorb_nbr = .) then comorb_nbr = 0;

keep DUPERSID tot_exp age educyr income female black hispc otherace married metro 
CURR_SMK exercise INSCOV dep_anx_sz employ comorb_nbr; 
run;



data  try;
merge adult_tot_05(in=A) add;
by dupersid;
if A;
if occcat>0 and indcat>0;
drop OCCCAT31 OCCCAT42 OCCCAT53 INDCAT31 INDCAT42 INDCAT53;
run;

data  meps2005.final;
set try;
if occcat in (1,2) then occcat=1; /* management, professional */
else occcat=0;

if indcat in (1,3,12) then indcat=1; /* services, agriculture, services */
else indcat=0;
run;

proc freq data=meps2005.final;
tables inscov;
where occcat=0 and indcat=1;
run;

proc sort data=try out=sorted;
by occcat;
run;
proc freq data=sorted;
tables inscov;
by occcat;
run; 

proc sort data=try out=sorted;
by indcat;
run;
proc freq data=sorted;
tables inscov;
by indcat;
run; 
